package com.chenzhou.product_sys.dao.impl;

import com.chenzhou.product_sys.common.BaseDAO;
import com.chenzhou.product_sys.entity.GoodsEntity;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class GoodsDAOImpl extends BaseDAO<GoodsEntity>{
    @Override
    protected List<GoodsEntity> resultSetToList(ResultSet rs) throws SQLException {
        List<GoodsEntity> list = new ArrayList<>();
        while(rs.next()){
            GoodsEntity goodsEntity = new GoodsEntity();
            goodsEntity.setId(rs.getInt("id"));
            goodsEntity.setGname(rs.getString("gname"));
            goodsEntity.setGoodsInfo(rs.getString("goodsInfo"));
            goodsEntity.setPrice(rs.getFloat("price"));
            goodsEntity.setNum(rs.getInt("num"));
            goodsEntity.setGoods_deltis(rs.getString("goods_deltis"));
            goodsEntity.setGoods_type(rs.getInt("goods_type"));
            goodsEntity.setMarkup(rs.getString("markup"));
            goodsEntity.setType_name(rs.getString("type_name"));
            list.add(goodsEntity);
        }
        return list;
    }

    public List<GoodsEntity> queryAllGoods(){
        String sql = "select g.*,gt.type_name from tbl_goods g left join tbl_goods_type gt " +
                "on g.goods_type=gt.id";
        return this.execute_Query(sql,null);
    }

    public GoodsEntity queryGoodsByID(Integer id){
        String sql =  "select g.*,gt.type_name from tbl_goods g left join tbl_goods_type gt " +
                "on g.goods_type=gt.id where g.id=?";
        Object[] objects = new Object[]{id};
        List<GoodsEntity> list = this.execute_Query(sql,objects);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    public int add_GoodsEntity(GoodsEntity goodsEntity){
        String sql = "insert into tbl_goods values(null,?,?,?,?,?,?,?)";
        Object[] objects = new Object[]{
                goodsEntity.getGname(),
                goodsEntity.getGoodsInfo(),
                goodsEntity.getPrice(),
                goodsEntity.getNum(),
                goodsEntity.getGoods_deltis(),
                goodsEntity.getGoods_type(),
                goodsEntity.getMarkup()
        };
        return this.execute_modify(sql,objects);
    }

    public int update_GoodsEntity(GoodsEntity goodsEntity){
        String sql = "update tbl_goods set gname=?,goodsInfo=?,price=?" +
                ",num=?,goods_deltis=?,goods_type=?,markup=? where id=?";
        Object[] objects = new Object[]{
                goodsEntity.getGname(),
                goodsEntity.getGoodsInfo(),
                goodsEntity.getPrice(),
                goodsEntity.getNum(),
                goodsEntity.getGoods_deltis(),
                goodsEntity.getGoods_type(),
                goodsEntity.getMarkup(),
                goodsEntity.getId()
        };
        return this.execute_modify(sql,objects);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete_GoodsEntity(Integer id){
        String sql = "delete from tbl_goods where id = ?";
        Object[] objects = new Object[]{id};
        return this.execute_modify(sql,objects);
    }
}
